package com.lemon.springcloud.core.listener.def;


import com.lemon.springcloud.core.config.LemonSpringBootConfig;
import com.lemon.springcloud.core.initial.LemonApplicationInitializer;
import com.lemon.springcloud.core.listener.LemonBootStarterListener;
import com.sun.org.apache.xpath.internal.operations.Bool;
import org.springframework.context.ApplicationContext;
import java.util.LinkedHashMap;
import java.util.Map;

/**
 * @author hdzhang
 * @ClassName LemonStarterReporterListener
 * @desc
 * @Date 2018/9/7 21:34
 * @Version 1.0
 **/
public class LemonStarterReporterListener implements LemonBootStarterListener {

    private static final String TAB_SIGN = "\t";

    private Map<String, Object> extInfo = new LinkedHashMap<String, Object>();

    @Override
    public String starterName() {
        return "lemon-spring-boot-starter";
    }

    @Override
    public Map<String, Object> starterInfoCollect() {
        return extInfo;
    }

    @Override
    public void onStarting() {

    }

    @Override
    public void onPrepared(ApplicationContext context) {

    }

    @Override
    public void onFinished(ApplicationContext context) {
        Boolean mybatisEnable = LemonApplicationInitializer.getLemonBuildMybatisEnable();
        Boolean redisEnable = LemonApplicationInitializer.getLemonBuildRedisEnable();
        Boolean rabbitmqEnable = LemonApplicationInitializer.getLemonBuildRabbitMQEnable();
        Boolean zkEnable = LemonApplicationInitializer.getLemonBuildInZkEnable();
        Boolean esEnable = LemonApplicationInitializer.getLemonBuildInEsEnable();

        if (mybatisEnable) {
            extInfo.put("lemon-mybatis-enabled", mybatisEnable);
            LinkedHashMap<String, String> mybatisGroupInfo = LemonSpringBootConfig.getMybatisGroupInfo();
            for (String groupName : mybatisGroupInfo.keySet()) {
                extInfo.put(TAB_SIGN + groupName + "-Mybatis", mybatisGroupInfo.get(groupName));
            }
        }

        if (redisEnable) {
            extInfo.put("lemon-redis-enabled", redisEnable);
            LinkedHashMap<String, String> redisGroupInfo = LemonSpringBootConfig.getRedisGroupInfo();
            for (String groupName : redisGroupInfo.keySet()) {
                extInfo.put(TAB_SIGN + groupName + "-RedisCache", redisGroupInfo.get(groupName));
            }
        }

        if (rabbitmqEnable) {
            extInfo.put("lemon-rabbitmq-enabled", rabbitmqEnable);
            LinkedHashMap<String, String> rabbitmqGroupInfo = LemonSpringBootConfig.getRabbitmqGroupInfo();
            for (String groupName : rabbitmqGroupInfo.keySet()) {
                extInfo.put(TAB_SIGN + groupName + "-RabbitMQ", rabbitmqGroupInfo.get(groupName));
            }
        }

        if (zkEnable) {
            extInfo.put("lemon-zookeeper-enabled", zkEnable);
            LinkedHashMap<String, String> zookeeperGroupInfo = LemonSpringBootConfig.getZookeeperGroupInfo();
            for (String groupName : zookeeperGroupInfo.keySet()) {
                extInfo.put(TAB_SIGN + groupName + "-ZK", zookeeperGroupInfo.get(groupName));
            }
            /*extInfo.put("\tlemon-zookeeper-config", LemonSpringBootConfig.BUILD_IN_ZOOKEEPER + " " +
                    (LemonApplicationInitializer.isZkConnected() ? "[connected]" : "lost"));*/
        }

        if (esEnable) {
            extInfo.put("lemon-es-enabled", esEnable);
            LinkedHashMap<String, String> zookeeperGroupInfo = LemonSpringBootConfig.getElasticsearchGroupInfo();
            for (String groupName : zookeeperGroupInfo.keySet()) {
                extInfo.put(TAB_SIGN + groupName + "-ElasticsearchTool", zookeeperGroupInfo.get(groupName));
            }
        }
    }
}
